﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.ComponentModel;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all controls are used to send commands from the user to the server.
	/// </summary>
	[SupportsEventValidation]
	[DefaultEvent("Click")]
	[ParseChildren(true)]
	[PersistChildren(false)]
	public abstract class BaseCommandControl : BaseSimpleValueControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public BaseCommandControl()
		{
			this.Tag = HtmlTextWriterTag.Input;
			this.RenderIdAttribute = true;
		}
		#endregion
		#region EVENTS
		/// <summary>
		/// Raised when the control is clicked.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public virtual ServerEvent<EventArgs> Click { get { return this.GetServerEvent<EventArgs>("Click"); } }
		#endregion
		#region PROPS
		/// <summary>
		/// Gets or sets a flag which indicates whether or not to render the ID attribute.
		/// </summary>
		[DefaultValue(true)]
		public override bool RenderIdAttribute { get; set; }
		/// <summary>
		/// Gets or sets the HtmlTextWriterTag of this control.
		/// </summary>
		[DefaultValue(HtmlTextWriterTag.Input)]
		public override HtmlTextWriterTag Tag
		{
			get
			{
				return base.Tag;
			}
			set
			{
				base.Tag = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers <see cref="Script.Turbo"/>.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(Script.Turbo);
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds "onclick" attribute.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			if (this.Enabled)
			{
				string script = this.Click.GetEventScript(null);
				if (!string.IsNullOrEmpty(script))
					writer.AddAttribute(HtmlTextWriterAttribute.Onclick, script);
			}
			base.AddAttributesToRender(writer);
		}
		protected override bool IsNameAttributeRendered()
		{
			return true;
		}
		#endregion
		#region EVENTS
		/// <summary>
		/// Raises events.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="argument"></param>
		public override void RaiseEvent(string name, string argument)
		{
			switch (name)
			{
				case "Click":
					OnClick(EventArgs.Empty);
					return;
			}
			base.RaiseEvent(name, argument);
		}
		/// <summary>
		/// Raises the <see cref="Click"/> event.
		/// </summary>
		/// <param name="e"></param>
		protected virtual void OnClick(EventArgs e)
		{
			Click.Raise(this, e);
		}
		#endregion
		#endregion
		#endregion
	}
}